package com.uaroadshow.finale.web.controller.trading;

import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.web.utils.WebUtils;
import com.demo2do.core.wechat.entity.cfg.JsConfig;
import com.uaroadshow.entity.user.User;
import com.uaroadshow.finale.processor.WechatProcessor;
import com.uaroadshow.finale.web.interceptor.UserContext;
import com.uaroadshow.finale.web.interceptor.UserContextType;
import com.uaroadshow.finale.web.resolver.Secure;

/**
 * 
 * @author mohanlan
 *
 */
@Controller
@RequestMapping("/shopping")
public class TradingController {
    
    @Autowired
    private WechatProcessor wechatProcessor;
    
    /**
     * 
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    @UserContext(type = UserContextType.WECHAT_AUTHORITY)
    public ModelAndView prepare(@Secure User user,
                                @RequestParam(name = "sku") Long productId,
                                HttpServletRequest httpServletRequest) {
        
        ModelAndView modelAndView = new ModelAndView("trading/shopping-prepare");
        
        String curl = WebUtils.getRealRequestURL(httpServletRequest);
        JsConfig jsConfig = wechatProcessor.generateJsConfig(curl);
        modelAndView.addObject("jsConfig", JsonUtils.toJson(jsConfig));
        
        modelAndView.addObject("user", user);
        
        return modelAndView;
        
    }
    
    /**
     * 
     * @param orderId
     * @return
     */
    @RequestMapping(value = "/empty", method = RequestMethod.GET)
    @UserContext(type = UserContextType.WECHAT_AUTHORITY)
    public ModelAndView empty(@Secure User user,
                              HttpServletRequest httpServletRequest) {
        
        ModelAndView modelAndView = new ModelAndView("trading/shopping-empty");
        
        String curl = WebUtils.getRealRequestURL(httpServletRequest);
        JsConfig jsConfig = wechatProcessor.generateJsConfig(curl);
        modelAndView.addObject("jsConfig", JsonUtils.toJson(jsConfig));
        
        return modelAndView;
    }
    
    /**
     * 
     * @param orderId
     * @return
     */
    @RequestMapping(value = "/{orderId}/result", method = RequestMethod.GET)
    @UserContext(type = UserContextType.WECHAT_AUTHORITY)
    public ModelAndView result(@Secure User user,
                               @PathVariable(name = "orderId") Long orderId,
                               HttpServletRequest httpServletRequest) {
        ModelAndView modelAndView = new ModelAndView("trading/shopping-result");
        modelAndView.addObject("orderId", orderId);
        
        String curl = WebUtils.getRealRequestURL(httpServletRequest);
        JsConfig jsConfig = wechatProcessor.generateJsConfig(curl);
        modelAndView.addObject("jsConfig", JsonUtils.toJson(jsConfig));
        
        modelAndView.addObject("user", user);
        
        return modelAndView;
    }
}
